package com.geqian.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geqian.common.common.PageRequest;
import com.geqian.common.common.PageResult;
import com.geqian.common.common.ResponseResult;
import com.geqian.web.utils.JwtUtils;
import com.geqian.web.entity.${className};
import com.geqian.web.mapper.${className}Mapper;
import com.geqian.web.service.${className}Service;
import com.geqian.web.common.dto.${className}Dto;
import com.geqian.web.utils.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.Objects;
import org.springframework.beans.BeanUtils;
import java.util.Collections;

/**
* ${description}服务实现
* @author ${author}
* @date ${date}
*/
@Service
public class ${className}ServiceImpl extends ServiceImpl<${className}Mapper, ${className}> implements ${className}Service {

    @Override
    public ResponseResult<PageResult<${className}>> get${className}List(PageRequest pageRequest) {
        LambdaQueryWrapper<${className}> wrapper = new LambdaQueryWrapper<>();
        String key = pageRequest.getKey();
        if (StringUtils.hasText(key)) {
        }
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        List<${className}> ${className?uncap_first}List = this.baseMapper.selectList(wrapper);
        PageInfo<${className}> pageInfo = new PageInfo<>(${className?uncap_first}List);
        PageResult<${className}> pageResult = PageUtils.getPageResult(pageInfo);
        return ResponseResult.success(pageResult);
    }

    @Override
    public ResponseResult<String> update${className}(${className}Dto ${className?uncap_first}Dto, String token) {
        if (Objects.nonNull(${className?uncap_first}Dto)) {
            try {
                ${className} ${className?uncap_first} = new ${className}();
                BeanUtils.copyProperties(${className?uncap_first}Dto, ${className?uncap_first});
                String userId = JwtUtils.parseForValue(token, "userId", String.class);
                this.baseMapper.updateBatch(Collections.singletonList(${className?uncap_first}));
            } catch (Exception e) {
                e.printStackTrace();
                return ResponseResult.fail("更新失败");
            }
        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult<String> add${className}(${className}Dto ${className?uncap_first}Dto, String token) {
        if (Objects.nonNull(${className?uncap_first}Dto)) {
            try {
                String userId = JwtUtils.parseForValue(token, "userId", String.class);
                ${className} ${className?uncap_first} = new ${className}();
                BeanUtils.copyProperties(${className?uncap_first}Dto, ${className?uncap_first});
                this.baseMapper.insertBatch(Collections.singletonList(${className?uncap_first}));
        } catch (Exception e) {
                e.printStackTrace();
                return ResponseResult.fail("新增失败");
            }
        }
        return ResponseResult.success();
    }


    @Override
    public ResponseResult<String> delete${className}(List<${className}> ${className?uncap_first}List) {
        if (${className?uncap_first}List != null && !${className?uncap_first}List.isEmpty()) {
            try {
                this.baseMapper.deleteBatch(${className?uncap_first}List);
            } catch (Exception e) {
                e.printStackTrace();
                return ResponseResult.fail("删除失败");
            }
        }
            return ResponseResult.success();
    }

}
